import {AfterViewInit, Component, OnInit, ViewChild} from '@angular/core';
import {MessageService, ModalComponent} from '../../../../../ng-tools';
import {TicketService} from '../../../../services/api/ticket.service';
import {Router} from '@angular/router';
import {HttpParams} from '@angular/common/http';
import {FormBuilder} from '@angular/forms';

@Component({
  selector: 'system-set',
  templateUrl: './system-set.component.html',
  styleUrls: ['./system-set.component.scss']
})
export class SystemSetComponent implements OnInit, AfterViewInit {
  breadcrumb = [
    {name: '系统配置', cssIcon: 'fa fa-fw fa-wrench'}
  ];
  @ViewChild('modal') modal: ModalComponent;
  isStartRegist = 0;
  isStartInvot = 0;
  configValue;
  configResume;
  configUrl;
  constructor(private message: MessageService,
              private router: Router,
              private formBuilder: FormBuilder,
              private ticketService: TicketService) {
  }

  ngOnInit() {
    this.initData();
  }
  initData() {
    this.ticketService.get(this.ticketService.urls.getInitData, {
      observe: 'response',
      params: new HttpParams().append('web_token', localStorage.getItem('token'))
    }).subscribe((resp) => {
      this.message.sendMessage('page load progress', this);
      const result = resp.body;
      if (result.success) {
        this.isStartRegist = result.returnValue.loginRegistConfigs[0].status;
        // if (result.returnValue.loginRegistConfigs[0].status === 1) {
        //   this.isStartRegist = true;
        // }
        this.isStartInvot = result.returnValue.loginRegistConfigs[1].status;
        // if (result.returnValue.loginRegistConfigs[1].status === 1) {
        //   this.isStartInvot = true;
        // }
        this.configValue = result.returnValue.loginRegistConfigs[3].configValue;
        this.configResume = result.returnValue.loginRegistConfigs[4].configValue;
      } else {
        this.router.navigateByUrl('/login').catch();
      }
    });
    this.ticketService.get(this.ticketService.urls.getUrl, {
      observe: 'response',
      params: new HttpParams().append('web_token', localStorage.getItem('token'))
    }).subscribe((resp1) => {
      if (resp1.body.success) {
        this.configUrl = resp1.body.returnValue.url;
      } else {
        this.router.navigateByUrl('/login').catch();
      }
    });
  }
  updateConfig(id, status, required, configValue) {
    if (id === 4 && configValue < 100) {
      const options = {
        id: 'systemSet',
        title: '金额提醒',
        cssIcon: 'fa fa-fw fa-warning',
        size: 'sm',
        theme: 'success',
        backdrop: 'static',
        data: '限制金额必须大于或等于100！',
        closeBtn: false,
        confirmBtn: true
      };
      this.modal.show(options);
      return;
    }
    this.ticketService.post(this.ticketService.urls.updateSysConfig, {id: id, status: status, required: required, config_value: configValue}, {
      observe: 'response',
      params: new HttpParams().append('web_token', localStorage.getItem('token'))}).subscribe((resp) => {
      if (resp.body.success) {
        if (id === 4 || id === 5) {
            const options = {
              id: 'systemSet',
              title: '系统配置',
              cssIcon: 'fa fa-fw fa-warning',
              size: 'sm',
              theme: 'success',
              backdrop: 'static',
              data: '系统配置修改成功！',
              closeBtn: false,
              confirmBtn: true
            };
            this.modal.show(options);
        }
        this.initData();
      } else {
        this.router.navigateByUrl('/login').catch();
      }
    });
  }

  updateUrl(configUrl) {
    this.ticketService.post(this.ticketService.urls.updateUrl, {'url': configUrl}, {
      observe: 'response',
      params: new HttpParams().append('web_token', localStorage.getItem('token'))}).subscribe((resp) => {
      if (resp.body.success) {
        const options = {
          id: 'systemSet',
          title: '系统配置',
          cssIcon: 'fa fa-fw fa-warning',
          size: 'sm',
          theme: 'success',
          backdrop: 'static',
          data: '系统配置修改成功！',
          closeBtn: false,
          confirmBtn: true
        };
        this.modal.show(options);
        this.initData();
      } else {
        this.router.navigateByUrl('/login').catch();
      }
    });
  }

  ngAfterViewInit(): void {
    this.modal.event.subscribe({
      next: (event) => {
        if (event.name === 'confirm' && event.id === 'systemSet') {
          this.modal.hide();
        }
      }
    });
    this.message.sendMessage('breadcrumb changed', this, this.breadcrumb);
    this.message.sendMessage('page load progress', this, '300ms');
  }

}
